<template>
  <div v-if="visible" class="thinking-overlay" @click="closeModal">
    <div class="thinking-modal" @click.stop>
      <div class="thinking-header">
        <div class="thinking-title">
          <span class="thinking-icon">🧠</span>
          <span class="thinking-text">AI REASONING PROCESS</span>
        </div>
        <div class="thinking-status">
          <span class="status-indicator" :class="{ active: isThinking }"></span>
          <span class="status-text">{{ isThinking ? 'THINKING...' : 'COMPLETE' }}</span>
        </div>
      </div>
      
      <div class="thinking-content">
        <div class="thinking-display">
          <pre class="thinking-text-content">{{ thinkingContent }}</pre>
          <div v-if="isThinking" class="thinking-cursor">█</div>
        </div>
      </div>
      
      <div class="thinking-footer">
        <div class="matrix-lines">
          <div class="matrix-line" v-for="i in 3" :key="i"></div>
        </div>
        <button @click="closeModal" class="close-btn">
          <span class="close-icon">×</span>
          CLOSE
        </button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, watch, onMounted, onUnmounted, nextTick } from 'vue'

const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  thinkingContent: {
    type: String,
    default: ''
  },
  isThinking: {
    type: Boolean,
    default: false
  },
  autoClose: {
    type: Boolean,
    default: true
  },
  autoCloseDelay: {
    type: Number,
    default: 3000 // 3秒后自动关闭
  }
})

const emit = defineEmits(['update:visible', 'close'])

let autoCloseTimer = null

// 监听思考状态变化
watch(() => props.isThinking, (newVal, oldVal) => {
  // 当思考结束时，启动自动关闭定时器
  if (oldVal && !newVal && props.autoClose) {
    autoCloseTimer = setTimeout(() => {
      closeModal()
    }, props.autoCloseDelay)
  }
  
  // 当开始思考时，清除自动关闭定时器
  if (!oldVal && newVal && autoCloseTimer) {
    clearTimeout(autoCloseTimer)
    autoCloseTimer = null
  }
})

// 监听可见性变化
watch(() => props.visible, (newVal) => {
  if (!newVal && autoCloseTimer) {
    clearTimeout(autoCloseTimer)
    autoCloseTimer = null
  }
})

// 监听思考内容变化，自动滚动到底部
watch(() => props.thinkingContent, () => {
  nextTick(() => {
    const thinkingDisplay = document.querySelector('.thinking-content')
    if (thinkingDisplay) {
      thinkingDisplay.scrollTop = thinkingDisplay.scrollHeight
    }
  })
})

const closeModal = () => {
  if (autoCloseTimer) {
    clearTimeout(autoCloseTimer)
    autoCloseTimer = null
  }
  emit('update:visible', false)
  emit('close')
}

// 键盘事件处理
const handleKeydown = (e) => {
  if (e.key === 'Escape' && props.visible) {
    closeModal()
  }
}

onMounted(() => {
  document.addEventListener('keydown', handleKeydown)
})

onUnmounted(() => {
  document.removeEventListener('keydown', handleKeydown)
  if (autoCloseTimer) {
    clearTimeout(autoCloseTimer)
  }
})
</script>

<style scoped>
.thinking-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.9);
  backdrop-filter: blur(10px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  animation: fadeIn 0.3s ease-out;
}

.thinking-modal {
  background: linear-gradient(135deg, rgba(0, 0, 0, 0.95), rgba(0, 20, 0, 0.9));
  border: 2px solid #00ff00;
  border-radius: 12px;
  width: 90%;
  max-width: 800px;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  box-shadow: 
    0 0 50px rgba(0, 255, 0, 0.5),
    inset 0 0 30px rgba(0, 255, 0, 0.1);
  animation: matrixGlow 2s infinite alternate, slideIn 0.5s ease-out;
  position: relative;
  overflow: hidden;
}

.thinking-modal::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(0, 255, 0, 0.1), transparent);
  animation: scan 3s linear infinite;
}

.thinking-header {
  padding: 20px;
  border-bottom: 1px solid rgba(0, 255, 0, 0.3);
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: rgba(0, 255, 0, 0.05);
}

.thinking-title {
  display: flex;
  align-items: center;
  gap: 12px;
}

.thinking-icon {
  font-size: 24px;
  filter: grayscale(1) brightness(0) invert(1);
  opacity: 0.9;
}

.thinking-text {
  color: #00ff00;
  font-family: 'Courier New', monospace;
  font-weight: bold;
  font-size: 18px;
  text-shadow: 0 0 10px #00ff00;
  letter-spacing: 2px;
}

.thinking-status {
  display: flex;
  align-items: center;
  gap: 8px;
}

.status-indicator {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: #666;
  transition: all 0.3s ease;
}

.status-indicator.active {
  background: #00ff00;
  box-shadow: 0 0 15px #00ff00;
  animation: pulse 1.5s infinite;
}

.status-text {
  color: #00ff00;
  font-family: 'Courier New', monospace;
  font-size: 12px;
  font-weight: bold;
  text-shadow: 0 0 5px #00ff00;
}

.thinking-content {
  flex: 1;
  padding: 20px;
  overflow-y: auto;
  min-height: 200px;
  max-height: 400px;
}

.thinking-display {
  position: relative;
  background: rgba(0, 0, 0, 0.8);
  border: 1px solid rgba(0, 255, 0, 0.3);
  border-radius: 8px;
  padding: 15px;
  font-family: 'Courier New', monospace;
  color: #00ff00;
  font-size: 14px;
  line-height: 1.6;
  text-shadow: 0 0 5px #00ff00;
  min-height: 150px;
  position: relative;
  overflow: hidden;
}

.thinking-display::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: 
    repeating-linear-gradient(
      0deg,
      transparent,
      transparent 2px,
      rgba(0, 255, 0, 0.03) 2px,
      rgba(0, 255, 0, 0.03) 4px
    );
  pointer-events: none;
}

.thinking-text-content {
  margin: 0;
  white-space: pre-wrap;
  word-wrap: break-word;
  color: #00ff00;
  background: transparent;
  border: none;
  font-family: inherit;
  font-size: inherit;
  line-height: inherit;
  position: relative;
  z-index: 1;
}

.thinking-cursor {
  display: inline-block;
  color: #00ff00;
  animation: blink 1s infinite;
  font-family: 'Courier New', monospace;
  font-weight: bold;
  text-shadow: 0 0 10px #00ff00;
}

.thinking-footer {
  padding: 15px 20px;
  border-top: 1px solid rgba(0, 255, 0, 0.3);
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: rgba(0, 255, 0, 0.02);
}

.matrix-lines {
  display: flex;
  gap: 4px;
}

.matrix-line {
  width: 2px;
  height: 20px;
  background: #00ff00;
  opacity: 0.6;
  animation: matrixLine 2s infinite;
}

.matrix-line:nth-child(2) {
  animation-delay: 0.3s;
}

.matrix-line:nth-child(3) {
  animation-delay: 0.6s;
}

.close-btn {
  background: rgba(0, 0, 0, 0.9);
  border: 1px solid #00ff00;
  color: #00ff00;
  padding: 8px 16px;
  border-radius: 4px;
  font-family: 'Courier New', monospace;
  font-weight: bold;
  font-size: 12px;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 6px;
  text-shadow: 0 0 5px #00ff00;
}

.close-btn:hover {
  background: rgba(0, 255, 0, 0.1);
  box-shadow: 0 0 15px rgba(0, 255, 0, 0.5);
  transform: translateY(-2px);
}

.close-icon {
  font-size: 16px;
  font-weight: bold;
}

/* 滚动条样式 */
.thinking-content::-webkit-scrollbar {
  width: 8px;
}

.thinking-content::-webkit-scrollbar-track {
  background: rgba(0, 0, 0, 0.5);
  border-radius: 4px;
}

.thinking-content::-webkit-scrollbar-thumb {
  background: rgba(0, 255, 0, 0.3);
  border-radius: 4px;
  border: 1px solid rgba(0, 255, 0, 0.5);
}

.thinking-content::-webkit-scrollbar-thumb:hover {
  background: rgba(0, 255, 0, 0.5);
}

/* 动画效果 */
@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes slideIn {
  from {
    transform: translateY(-50px) scale(0.9);
    opacity: 0;
  }
  to {
    transform: translateY(0) scale(1);
    opacity: 1;
  }
}

@keyframes matrixGlow {
  0% {
    box-shadow: 
      0 0 50px rgba(0, 255, 0, 0.5),
      inset 0 0 30px rgba(0, 255, 0, 0.1);
  }
  100% {
    box-shadow: 
      0 0 70px rgba(0, 255, 0, 0.7),
      inset 0 0 40px rgba(0, 255, 0, 0.2);
  }
}

@keyframes scan {
  0% {
    left: -100%;
  }
  100% {
    left: 100%;
  }
}

@keyframes pulse {
  0%, 100% {
    opacity: 1;
    transform: scale(1);
  }
  50% {
    opacity: 0.7;
    transform: scale(1.2);
  }
}

@keyframes blink {
  0%, 50% {
    opacity: 1;
  }
  51%, 100% {
    opacity: 0;
  }
}

@keyframes matrixLine {
  0%, 100% {
    height: 20px;
    opacity: 0.6;
  }
  50% {
    height: 30px;
    opacity: 1;
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .thinking-modal {
    width: 95%;
    max-height: 90vh;
  }
  
  .thinking-header {
    padding: 15px;
    flex-direction: column;
    gap: 10px;
    align-items: flex-start;
  }
  
  .thinking-text {
    font-size: 16px;
  }
  
  .thinking-content {
    padding: 15px;
    max-height: 300px;
  }
  
  .thinking-display {
    padding: 12px;
    font-size: 12px;
  }
  
  .thinking-footer {
    padding: 12px 15px;
    flex-direction: column;
    gap: 10px;
  }
}
</style>